package com.market.util;

import java.io.File;

public class FileUtil {
	//���ε� ���
	//주암경로
	public static final String UPLOAD_PATH = "C:/javaWork/workspace_spring/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/thorns/upload";
	
	/**
	 * ���� �̸� ����, ������ �̸��� �޼ҵ峻���� ����
	 * */
	public static String rename(String filename) throws Exception{
		
		if(filename ==null) return null;
		String newName = 
Long.toString(System.currentTimeMillis())+(int)(Math.random()*50);
		
		return rename(filename, newName);
	}
	/**
	 * ���� �̸� ����, ������ �̸��� �޼ҵ� ȣ���� ������ ���� ����
	 * */
	public static String rename(String filename, String newName) 
	                                            throws Exception{
		
		if(filename == null) return null;

		//���ϸ��� ���ϴ� ������� �����ϱ�
		int idx = filename.lastIndexOf(".");
		String extention = "";
		String newFileName = "";

		if(idx != -1) {
			extention = filename.substring(idx);
		}
		// Ȯ���ڸ� ������ �� ���ϸ��� �Է��ؾ� �ϴµ� Ȯ���ڸ� �����ؼ�
		// �Է��ߴٸ� Ȯ���ڸ� �����ϰ� ���ϸ����� ����
		int newIdx = newName.lastIndexOf(".");
		if(newIdx !=-1){
			newName = newName.substring(0,newIdx);
		}

		newFileName = newName + extention.toLowerCase();
		
		return newFileName;
	}
	/**
	 * ���� ����
	 * */
	public static void removeFile(String filename){
		File file = new File(UPLOAD_PATH,filename);
		if(file.exists()) file.delete();
		
	}
}
